from flask import Blueprint, jsonify
from flask_restful import Api, Resource, reqparse,marshal

from models.models import *
from models import db
from utils.models_fields import *

teacher_bp = Blueprint("teacher",__name__)
api = Api(teacher_bp)


class Teac(Resource):
    #教师列表
    def get(self):
        user_all = Teacher.query.all()
        list1 = []
        for i in user_all:
            dict1 = {
                "id":i.id,
                "name":i.name,
                "attention_num":i.attention_num,
                "phone":i.phone,
                "password":i.password,
                "info":i.info,
                "create_time":i.create_time,
                "sttus":i.status,
                "photo":i.photo,
            }
            list1.append(dict1)
        return jsonify({
                "code":200,
                "msg":"返回成功",
                "data":list1
            })

    #查询单条数据教师
    def post(self):
        # print("---------------")
        req = reqparse.RequestParser()
        req.add_argument('id')
        args = req.parse_args()
        # print(args)
        req.add_argument("id")
        print(type(args['id']))

        teac_info = Teacher.query.filter(Teacher.id == args['id']).first()

        dict1 = {
            "id": teac_info.id,
            "name": teac_info.name,
            "attention_num": teac_info.attention_num,
            "phone": teac_info.phone,
            "password": teac_info.password,
            "info": teac_info.info,
            "create_time": teac_info.create_time,
            "sttus": teac_info.status,
            "photo": teac_info.photo,
        }
        return jsonify({
            "code":200,
            "msg":"查询单条老师数据成功",
            "data":dict1
        })
api.add_resource(Teac,"/teacher_all")

#课程对应的老师
class TeaCourse(Resource):
    def get(self):
        parser = reqparse.RequestParser()
        parser.add_argument('id')
        args = parser.parse_args()
        id= args.get('id')
        course = Course.query.filter_by(id=id).first()
        print(course)
        coursetea = course.tea
        print(coursetea)
        coursetea_ser = marshal(coursetea,teacher_fields)
        return jsonify(msg='获取成功',code=200,data=coursetea_ser)

api.add_resource(TeaCourse,'/teacourse')





